const {ccclass, property} = cc._decorator;

@ccclass
export default class GameManager extends cc.Component {
    private hero:cc.Node;
    private stick:cc.Node;

    @property(cc.Prefab)
    public platformPrefab:cc.Prefab=null
    
    
    private startPlatform:cc.Node;
    private endPlatform:cc.Node;

    private isGrow:boolean=false;
    private canAction:boolean=true;
    
    private OverPanel:cc.Node;

    private anim:cc.Animation;

    private scoreLabel:cc.Label;
    private _score:number=0;
    get score(){
        return this._score;
    }
    set score(value){
        this._score=value;
        this.scoreLabel.string = this.score.toString();
    }

    onLoad () {
        this.hero = this.node.getChildByName("hero");
        this.anim = this.hero.getComponent(cc.Animation);
        this.stick = this.node.getChildByName("stick");

        this.startPlatform = this.node.getChildByName("platform");
        this.scoreLabel = this.node.getChildByName("Score").getComponent(cc.Label);

        this.OverPanel = this.node.getChildByName("OverPanel");

        this.stick.height = 0;

        this.node.on(cc.Node.EventType.TOUCH_START,this.touchStart,this);
        this.node.on(cc.Node.EventType.TOUCH_END,this.touchEnd,this);
    }

    start () {
        this.revive();
    }

    update (dt) {      
        if(this.isGrow){
            this.stick.height+=3;
        }
    }

    touchStart(){        
        if(this.isGrow||!this.canAction)return;
        let p1 = this.startPlatform.position;
        p1.x += this.startPlatform.width/2-2.5
        p1.y += this.startPlatform.height-2.5;
        this.stick.rotation = 0;
        this.stick.height=0;
        this.stick.position = p1;       
        this.isGrow = true;
    }

    touchEnd(){    
        if(!this.canAction) return;
        this.isGrow = false; 
        this.canAction = false;
        this.Bridge();
    }

    /**
     * 生成桥
     */
    Bridge(){
       
        //计算旋转角度
        // cc.log(this.stick.position);
        // cc.log(this.endPlatform.position);

        // let p1 = this.stick.position;
        // let p2 = this.endPlatform.position;
        // p2.y+=this.endPlatform.height;


        let a1 = cc.delayTime(0.2);
        let a2 = cc.rotateTo(0.5,90);
        let a3 = cc.callFunc(this.Move,this);
        let seq = cc.sequence(a1,a2,a3);

        this.stick.runAction(seq);

    }

    /**
     * 移动
     */
    Move(){
        
        let leng = this.stick.position.x+this.stick.height;

        let a1 = cc.moveTo(2, leng,this.hero.y);
        let a2 = cc.callFunc(()=>{
            this.anim.setCurrentTime(0)
            this.anim.stop();
        })
        let a3 = cc.callFunc(this.check,this);
        let seq = cc.sequence(a1,a2,a3);
     
        this.anim.play();
     
        this.hero.runAction(seq);
    }

    /**
     * 检测
     */
    check(){
        let p1 = this.hero.position;
        p1.y-=this.hero.height/2;


        let p2 = this.endPlatform.position;
        let size = this.endPlatform.getContentSize();
        p2.x-=size.width/2;
        //p2.y+=size.height;

        let rect = new cc.Rect(p2.x,p2.y,size.width,size.height);

        if(rect.contains(p1)){
            cc.log("活")
            this.score++;
            this.next();
        }else{
           
            this.die();
            cc.log("死")
        }
        
    }

    /**
     * 移动柱子
     */
    next(){
        this.stick.height=0;
        this.startPlatform.removeFromParent();

        let size = this.endPlatform.getContentSize();
        let p1 = this.endPlatform.position;
        let endX = -cc.winSize.width/2;
        let offsex = p1.x-endX-size.width/2;

        let a1 = cc.moveBy(offsex/500,-offsex,0);        
        this.hero.runAction(a1);


        let a2 = cc.moveBy(offsex/500,-offsex,0);   
        let a3 = cc.delayTime(0.2);
        let a4 = cc.callFunc(this.revive,this);
        let seq = cc.sequence(a2,a3,a4);
        this.endPlatform.runAction(seq);     
        



    }

    /**
     * 再生一个柱子
     */
    revive(){
        if(this.endPlatform){
            this.startPlatform = this.endPlatform;
        }
        
    
        //let w = cc.winSize.width/3;
        //let x = Math.random()*(w+w+1)-w
        
        //计算X的位置
        let pw = Random(40,150);
        let ph = Random(100,1000);
        
        let x = this.startPlatform.x+this.startPlatform.width/2+pw/2;

        let px = Random(x+20,20);
        
      
        

        let node = cc.instantiate(this.platformPrefab);
        node.position = cc.v2(px,0);
        node.parent = this.node;
        node.width = pw;
        //node.height = ph;

        this.endPlatform = node;        

        this.canAction = true;
        this.hero.setSiblingIndex(100);
    }

    die(){
        
        let h = cc.winSize.height/2+this.hero.height;                
        let a1 = cc.moveTo(h/800,this.hero.x,-h);
        let a2 = cc.repeatForever(
            cc.rotateBy(0.1,80)
        );

        let seq = cc.spawn(a1,a2);
        this.hero.runAction(a2);

        this.schedule(()=>{
            this.hero.y-=10;
            if(this.hero.y<-cc.winSize.height/2-100){
                this.OverPanel.active = true
                this.unscheduleAllCallbacks();
            }            
        },0.01)
        let b1 = cc.rotateTo(0.6,180);
        this.stick.runAction(b1);
        
    }
}
